package com.codechuan.didi_web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.codechuan.didi_web.entity.SysUserOrder;
import com.codechuan.didi_web.mapper.SysUserOrderMapper;
import com.codechuan.didi_web.service.SysUserOrderService;
import com.codechuan.didi_web.utils.ResultUtils;
import com.codechuan.didi_web.vo.ResultVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysUserOrderServiceImpl implements SysUserOrderService {
    @Resource
    private SysUserOrderMapper sysUserOrderMapper;
    @Resource
    private RedisTemplate<String, String> redisTemplate;


    /**
     * @param account 账户/卡密
     * @return 返回查询到的数据
     * @Deprecated 查询用户自己的所有订单
     */
    @Override
    public ResultVO<Object> getMyOrderList(String account) {
        // 从redis中获取卡密
        String tokenData = redisTemplate.opsForValue().get(account);
        JSONObject jsonObject = JSONObject.parseObject(tokenData);
        assert jsonObject != null;
        String keyNumber = jsonObject.getString("keyNumber");
        List<SysUserOrder> orderList = sysUserOrderMapper.selectOrderListByAccount(keyNumber);
        return ResultUtils.success(orderList);
    }

    /**
     * @param orderId 订单编号
     * @Deprecated 删除用户的订单
     */
    @Override
    public ResultVO<Object> deleteOrder(Integer orderId) {
        sysUserOrderMapper.deleteByPrimaryKey(orderId);
        return ResultUtils.success(null,"删除成功") ;
    }
}
